package com.yonyou.sto.modules.ums.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.sto.common.api.CommonPage;
import com.yonyou.sto.common.api.CommonResult;
import com.yonyou.sto.common.domain.DownloadFile;
import com.yonyou.sto.modules.ums.dto.UmsAdminParam;
import com.yonyou.sto.modules.ums.model.UmsAdmin;
import com.yonyou.sto.modules.ums.model.UmsNcCustomer;
import com.yonyou.sto.modules.ums.model.UmsNcMategrade;
import com.yonyou.sto.modules.ums.service.UmsNcCustomerService;
import com.yonyou.sto.modules.ums.service.UmsNcMategradeService;
import com.yonyou.sto.modules.ums.service.impl.ConfigNameServicImpl;
import com.yonyou.sto.modules.ums.util.NcMateGradeUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aegon
 * @since 2022-11-25
 */
@RestController
@RequestMapping("/server/ums/umsNcMategrade")
public class UmsNcMategradeController {

    @Autowired
    private UmsNcMategradeService umsNcMategradeService;

    @Resource
    private ConfigNameServicImpl configNameServic;

    @ApiOperation(value = "根据付款单位分页查询客户信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsNcMategrade>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                         @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize,
                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<UmsNcMategrade> partyPage = umsNcMategradeService.list(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(partyPage));
    }

    /**
     * 查询物料分类主键
     */
    @ApiOperation(value = "查询客户名称列表供用户选择")
    @RequestMapping(value = "/idList",method = RequestMethod.GET)
    public CommonResult<List<String>> nameList(){
        List<String> names = umsNcMategradeService.getIds();
        return CommonResult.success(names);
    }

    @ApiOperation("获取指定用户信息")
    @RequestMapping(value = "/{pkMategrade}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsNcMategrade> getItem(@PathVariable String pkMategrade) {
        UmsNcMategrade umsNcMategrade = umsNcMategradeService.getById(pkMategrade);
        if (umsNcMategrade == null) return CommonResult.failed("未找到该用户!");
        List<String> mateName = NcMateGradeUtil.getMateName(umsNcMategrade);
        List<String> specModel = NcMateGradeUtil.getSpecModel(umsNcMategrade);
        umsNcMategrade.setMatename(mateName);
        umsNcMategrade.setSpecModel(specModel);
        return CommonResult.success(umsNcMategrade);
    }

    @ApiOperation("修改设置指定物料的开票名称")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable String id, @RequestBody UmsNcMategrade umsNcMategrade){
        UmsNcMategrade byId = umsNcMategradeService.getById(id);
        boolean result = umsNcMategradeService.update(byId,umsNcMategrade);
        if (result){
            configNameServic.setumsNcMategrade(byId);
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @ApiOperation(value = "导出物料关系")
    @GetMapping(value = "/downloadFile")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(value = "keyword", required = false) String keyword,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) throws Exception {
        String s = umsNcMategradeService.downloadDate(pageNum, pageSize, keyword);
        try {
            InputStream is = new FileInputStream(new File("/usr/local/sto/outfile/"+s));

//            InputStream is = this.getClass().getClassLoader().getResourceAsStream("s");
            byte[] fileData = DownloadFile.input2byte(is);
            DownloadFile.downloadFile(response, request, "ncMategradeTemplate.xlsx", fileData);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    @ApiOperation(value = "组织导入excel")
    @RequestMapping(value = "/importNcMategrade")
    public CommonResult importNcCusttax(MultipartFile file) {
        return umsNcMategradeService.importNcCusttax(file);
    }
}

